package com.lab.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.lab.common.core.domain.entity.SysUser;
import com.lab.common.core.domain.model.LoginUser;
import com.lab.common.enums.AuditEnum;
import com.lab.common.enums.LabBorrowStatus;
import com.lab.system.domain.Lab;
import com.lab.system.mapper.LabMapper;
import com.lab.system.service.ILabService;
import com.lab.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lab.system.mapper.LabBorrowMapper;
import com.lab.system.domain.LabBorrow;
import com.lab.system.service.ILabBorrowService;

/**
 * 实验室使用记录Service业务层处理
 *
 * @author cf
 * @date 2025-02-14
 */
@Service
public class LabBorrowServiceImpl implements ILabBorrowService {
    @Autowired
    private LabBorrowMapper labBorrowMapper;
    @Autowired
    private LabMapper labMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ILabService labService;

    /**
     * 查询实验室使用记录
     *
     * @param id 实验室使用记录主键
     * @return 实验室使用记录
     */
    @Override
    public LabBorrow selectLabBorrowById(Long id) {
        LabBorrow labBorrow = labBorrowMapper.selectLabBorrowById(id);
        Lab lab = labMapper.selectLabById(labBorrow.getLabId());
        labBorrow.setLabName(lab.getLabName());
        SysUser sysUser = sysUserService.selectUserById(labBorrow.getUserId());
        labBorrow.setUserName(sysUser.getUserName());
        labBorrow.setStatusDesc(LabBorrowStatus.getLabStatus(labBorrow.getStatus()).getInfo());
        labBorrow.setAuditDesc(AuditEnum.getLabStatus(labBorrow.getAudit()).getInfo());
        return labBorrow;
    }

    /**
     * 查询实验室使用记录列表
     *
     * @param labBorrow 实验室使用记录
     * @return 实验室使用记录
     */
    @Override
    public List<LabBorrow> selectLabBorrowList(LabBorrow labBorrow, LoginUser loginUser) {
        if (loginUser != null) {
            // 数据权限控制
            List<Lab> labs = labService.selectLabList(new Lab(), loginUser);
            List<Long> labIds = labs.stream().map(Lab::getId).collect(Collectors.toList());
            labBorrow.setLabIds(labIds);
        }
        List<LabBorrow> labBorrows = labBorrowMapper.selectLabBorrowList(labBorrow);
        for (LabBorrow labBorrow1 : labBorrows) {
            Lab lab = labMapper.selectLabById(labBorrow1.getLabId());
            labBorrow1.setLabName(lab.getLabName());
            SysUser sysUser = sysUserService.selectUserById(labBorrow1.getUserId());
            labBorrow1.setUserName(sysUser.getUserName());
            labBorrow1.setStatusDesc(LabBorrowStatus.getLabStatus(labBorrow1.getStatus()).getInfo());
            labBorrow1.setAuditDesc(AuditEnum.getLabStatus(labBorrow1.getAudit()).getInfo());
        }
        return labBorrows;
    }

    /**
     * 新增实验室使用记录
     *
     * @param labBorrow 实验室使用记录
     * @return 结果
     */
    @Override
    public int insertLabBorrow(LabBorrow labBorrow,Long userId) {
        // 查询labId对应的实验室信息
        List<LabBorrow> labBorrows = labBorrowMapper.selectLabBorrowList(labBorrow);
        if (!labBorrows.isEmpty()) {
            throw new RuntimeException("该实验室在"+ DateUtil.format(labBorrows.get(0).getStartTime(), "yyyy-MM-dd HH:mm:ss") +
                    "至" + DateUtil.format(labBorrows.get(0).getEndTime(), "yyyy-MM-dd HH:mm:ss") +"已被预约，请重新选择！");
        }
        // 判断开始结束时间
        if (labBorrow.getStartTime().getTime() >= labBorrow.getEndTime().getTime()) {
            throw new RuntimeException("结束时间不能早于开始时间！");
        }
        labBorrow.setUserId(userId);
        labBorrow.setStatus(0);
        return labBorrowMapper.insertLabBorrow(labBorrow);
    }

    /**
     * 修改实验室使用记录
     *
     * @param labBorrow 实验室使用记录
     * @return 结果
     */
    @Override
    public int updateLabBorrow(LabBorrow labBorrow) {
        return labBorrowMapper.updateLabBorrow(labBorrow);
    }

    /**
     * 批量删除实验室使用记录
     *
     * @param ids 需要删除的实验室使用记录主键
     * @return 结果
     */
    @Override
    public int deleteLabBorrowByIds(Long[] ids) {
        return labBorrowMapper.deleteLabBorrowByIds(ids);
    }

    /**
     * 删除实验室使用记录信息
     *
     * @param id 实验室使用记录主键
     * @return 结果
     */
    @Override
    public int deleteLabBorrowById(Long id) {
        return labBorrowMapper.deleteLabBorrowById(id);
    }

    @Override
    public List<LabBorrow> getAllAudit(Integer code) {
        LabBorrow labBorrow = new LabBorrow();
        labBorrow.setAudit(code);
        labBorrow.setStatus(LabBorrowStatus.USE_AUTO.getCode());
        return selectLabBorrowList(labBorrow,null);
    }
}
